﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Configuration;

namespace AnguaRPGEngine.Library.Forms
{
    public partial class BookEditor : Form
    {
        private RuleBook _Book;
        public RuleBook Book
        {
            get
            {

                return _Book;
            }
        }
        #region Overrides For Background Painting And Resizing

        //const int WM_NCHITTEST = 0x0084;
        //const int HTCLIENT = 1;
        //const int HTCAPTION = 2;
        //protected override void WndProc(ref Message m)
        //{
        //    base.WndProc(ref m);
        //    switch (m.Msg)
        //    {
        //        case WM_NCHITTEST:
        //            if (m.Result == (IntPtr)HTCLIENT)
        //            {
        //                m.Result = (IntPtr)HTCAPTION;
        //            }
        //            break;
        //    }
        //}
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //    e.Graphics.FillRectangle(new LinearGradientBrush(new Point(0, 0), new Point(this.Width, 15), Color.DarkBlue, Color.Blue), new Rectangle(0, 0, this.Width, 15));
        //    e.Graphics.DrawLine(new Pen(new SolidBrush(Color.DarkGray), 1f), new Point(0, 16), new Point(this.Width, 16));
        //    e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.White), new PointF(0, 0));

        //}
        //protected override CreateParams CreateParams
        //{
        //    get
        //    {
        //        CreateParams cp = base.CreateParams;
        //        cp.Style |= 0x40000;

        //        return cp;
        //    }
        //}
        #endregion
        public BookEditor(RuleBook book)
        {
            InitializeComponent();
            this._Book = book;
            if (book != null)
            {
                txtBookName.Text = book.BookName;
                txtBookFSpec.Text = book.FileName;
                DisplayBookmarks();
            }
            else
            {
                _Book = new RuleBook();
                _Book.Bookmarks = new List<Bookmark>();
            }
        }

        private void DisplayBookmarks()
        {
            lvBookmarks.Items.Clear();
            btnEditMark.Enabled = btnDeleteMark.Enabled = false;
            foreach (Bookmark mark in _Book.Bookmarks)
            {
                lvBookmarks.Items.Add(mark.Name).SubItems.Add(mark.Page);
            }
        }
        private void BookEditor_Load(object sender, EventArgs e)
        {
            btnEditMark.Enabled = btnDeleteMark.Enabled = false;
        }

        private void lvBookmarks_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnEditMark.Enabled = btnDeleteMark.Enabled = lvBookmarks.SelectedItems.Count > 0;
        }

        private void txtBookName_TextChanged(object sender, EventArgs e)
        {
            _Book.BookName = txtBookName.Text;
        }

        private void txtBookFSpec_TextChanged(object sender, EventArgs e)
        {
            _Book.FileName = txtBookFSpec.Text;
        }

        private void btnAddMark_Click(object sender, EventArgs e)
        {
            using (BookmarkEditor frmBookmarkEditor = new BookmarkEditor(_Book.FileName, null))
            {
                if (frmBookmarkEditor.ShowDialog() == DialogResult.OK)
                {
                    _Book.Bookmarks.Add(frmBookmarkEditor.Bookmark);
                    DisplayBookmarks();
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Title = "Select Rule Book";
                ofd.Filter = "Adobe Acrobat Files (*.pdf)|*.pdf|Text Files (*.txt)|*.txt";
                ofd.AddExtension = true;
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    txtBookFSpec.Text = ofd.FileName;
                }

            }
        }

        private void btnEditMark_Click(object sender, EventArgs e)
        {
            Bookmark bookmark = _Book.Bookmarks.Find(p => p.Name == lvBookmarks.SelectedItems[0].Text);
            using (BookmarkEditor frmBookmarkEditor = new BookmarkEditor(_Book.FileName, bookmark))
            {
                if (frmBookmarkEditor.ShowDialog() == DialogResult.OK)
                {
                    bookmark.Name = frmBookmarkEditor.Bookmark.Name;
                    bookmark.OpenCommand = frmBookmarkEditor.Bookmark.OpenCommand;
                    bookmark.Page = frmBookmarkEditor.Bookmark.Page;
                    DisplayBookmarks();
                }
            }
        }

        private void btnDeleteMark_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "Are you certain you wish to delete this bookmark?", "Confirm Delete", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            {
                _Book.Bookmarks.RemoveAt(lvBookmarks.SelectedItems[0].Index);
                DisplayBookmarks();
            }
        }

    
    }
}
